package com.doudou.collection.service.impl;

import com.doudou.collection.mapper.CommentsMapper;
import com.doudou.collection.pojo.dto.CommentsDTO;
import com.doudou.collection.pojo.entity.Comments;
import com.doudou.collection.pojo.vo.CommentBlogVO;
import com.doudou.collection.response.ResultVO;
import com.doudou.collection.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentsMapper commentsMapper;


    /**
     * 文章中发布评论
     * @param commentsDTO
     * @return
     */
    @Override
    public ResultVO insertComment(CommentsDTO commentsDTO){
        log.debug("用户发布的评论 {}", commentsDTO);
        // 存入数据表:实体类Comments
        Comments comments = new Comments();
        // 复制 + 赋值
        BeanUtils.copyProperties(commentsDTO, comments);
        comments.setCreateTime(new Date());
        commentsMapper.insertComment(comments);
        log.info("用户发布评论成功");
        return ResultVO.ok();

    }


    /**
     * 文章中删除评论
     * @param id
     * @return
     */
    @Override
    public ResultVO delete(@PathVariable Long id) {
        log.debug("正在处理根据评论id删除评论功能,评论id为 {}", id);
        commentsMapper.deleteById(id);
        return ResultVO.ok();
    }

    /**
     * 根据文章id查询评论列表
     * @param id
     * @return
     */
    @Override
    public ResultVO list(Long id){
        log.debug("正在处理根据文章id查询评论列表功能,文章id为 {}", id);
        List<CommentBlogVO> list = commentsMapper.selectAllById(id);
        return ResultVO.ok(list);
    }


    /**
     * 根据用户id查询个人评论列表
     * @param id
     * @return
     */
    @Override
    public ResultVO listing(Long id){
        log.debug("正在处理根据用户id查询个人评论列表功能,用户id为 {}", id);
        List<CommentBlogVO> listing = commentsMapper.selectPersonalAll(id);
        log.info("返回参数:{}",listing);
        return ResultVO.ok(listing);
    }


}
